﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.util;
using Imaginecup.sprite.changeSourceSize;
using Imaginecup.sprite;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.util;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util.collision;

namespace Imaginecup.stage
{
    public class ChoiceBox : ISourceInfoClass
    {
        private EntityInfo m_Info;
        private ISprite m_Sprite;
        private bool m_bEnable;

        private static Vector2 m_SourceSize = new Vector2(105, 104);

        private DrawNumber m_Number;
        private int m_StageNum;

        private Vector2 m_NumGab = Vector2.Zero;

        public ChoiceBox(int stageNum , bool enable)
        {
            m_bEnable = enable;
            m_StageNum = stageNum;
        }

        public void SetDrawNumber(DrawNumber number)
        {
            m_Number = number;
        }

        public void SetSprite(ISprite sprite)
        {
            m_Sprite = sprite;
        }

        public void SetEntityInfo(EntityInfo info)
        {
            m_Info = info;
        }

        public EntityInfo GetEntityInfo()
        {
            return m_Info;
        }

        public void SetEnable()
        {
            m_bEnable = true;
        }

        public bool GetEnable()
        {
            return m_bEnable;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            m_Sprite.Draw(spriteBatch , m_Info);

            if( m_bEnable )
            {
                String num = m_StageNum.ToString();
                m_Number.Draw(spriteBatch, GetNumberPositionX(num) + (int)m_NumGab.X, GetNumberPositionY() + (int)m_NumGab.Y, num);
            }
        }

        private int GetNumberPositionX(String num)
        {
            int centerX = (int)m_Info.GetEntityCenter().X;
            int numLength = num.Length;


            int drawNumberX = centerX - (int)((m_Number.GetNumberSize().X * numLength) / 2);

            return drawNumberX;
        }

        private int GetNumberPositionY()
        {
            int centerY = (int)m_Info.GetEntityCenter().Y;
            int drawNumberY = centerY - (int)(m_Number.GetNumberSize().Y  / 2);
            return drawNumberY;
        }

        public void Release()
        {
            m_Sprite.Release();
        }

        public EntityInfo GetSourceInfo()
        {
            if (m_bEnable)
            {
                IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

                m_NumGab.X = 2;
                m_NumGab.Y = 2;

                if( mouseInput.MouseState.LeftButton == ButtonState.Pressed && Collision.CollisionByPoint(m_Info , mouseInput.Position ) )
                {
                    return new EntityInfo(new Vector2(1 * m_SourceSize.X, 0), m_SourceSize);
                }
#if WINDOWS
                else if ( Collision.CollisionByPoint(m_Info, mouseInput.Position))
                {
                    return new EntityInfo(new Vector2(2 * m_SourceSize.X, 0), m_SourceSize);
                }
#endif
                m_NumGab = Vector2.Zero;
                return new EntityInfo(new Vector2(0, 0), m_SourceSize);
            }
            return new EntityInfo(new Vector2(3 * m_SourceSize.X , 0), m_SourceSize);
        }
    }
}
